# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=jasper
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.0.0
pkgrel=1
pkgdesc="Reference implementation of JPEG-2000 Part-1 codec (ISO/IEC 15444-5) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.ece.uvic.ca/~mdadams/jasper"
license=("spdx:JasPer-2.0")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
depends=("${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
options=('staticlibs' 'strip' '!libtool')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/mdadams/jasper/archive/version-${pkgver}.tar.gz
        # https://github.com/archlinux/svntogit-packages/blob/804b2dacf632d1c51aef527f591c0cbcd6d1c489/trunk/jasper-1.900.1-fix-filename-buffer-overflow.patch
        jasper-1.900.1-fix-filename-buffer-overflow.patch
        001-mingw-cmake.patch
        004-jasper-exports.patch)
sha256sums=('977c4c2e4210f4e37313cd2232d99e73d57ab561917b3c060bcdd5e83a0a13f1'
            'f51377e9b3e4faaa6b17b2d5fcf6f6d94fe2916a65dc9c78b5a99b891f5726dc'
            '89f7653884c93e1cb32271c59213551d1231674bd12df00331c302971d5ac489'
            '0f9b9270edb3f19c08425d8e97588b4ebe6b494579e2b66bb14929d01c8b4059')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "$srcdir"/${_realname}-version-${pkgver}

  apply_patch_with_msg \
    jasper-1.900.1-fix-filename-buffer-overflow.patch \
    001-mingw-cmake.patch \
    004-jasper-exports.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  msg2 "Building static library"
  [[ -d "${srcdir}"/build-${MSYSTEM}-static ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-static
  mkdir -p "${srcdir}"/build-${MSYSTEM}-static && cd "${srcdir}"/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_LIBDIR=lib \
      ${_extra_config[@]} \
      -DJAS_ENABLE_OPENGL=OFF \
      -DJAS_ENABLE_LIBJPEG=ON \
      -DJAS_ENABLE_AUTOMATIC_DEPENDENCIES=OFF \
      -DCMAKE_SKIP_RPATH=ON \
      -DJAS_ENABLE_SHARED=OFF \
      ../${_realname}-version-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .

  msg2 "Building shared library"
  [[ -d "${srcdir}"/build-${MSYSTEM}-shared ]] && rm -rf "${srcdir}"/build-${MSYSTEM}-shared
  mkdir -p "${srcdir}"/build-${MSYSTEM}-shared && cd "${srcdir}"/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_INSTALL_LIBDIR=lib \
      ${_extra_config[@]} \
      -DJAS_ENABLE_OPENGL=OFF \
      -DJAS_ENABLE_LIBJPEG=ON \
      -DJAS_ENABLE_AUTOMATIC_DEPENDENCIES=OFF \
      -DCMAKE_SKIP_RPATH=ON \
      -DJAS_ENABLE_SHARED=ON \
      ../${_realname}-version-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}-static
  ctest . || msg2 "tests failed"
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm 644 "${srcdir}/${_realname}-version-${pkgver}/LICENSE.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
